//------------------------------------------------------------------------------------------------- 
// <copyright file="SublayoutBase.cs" company="Sitecore Shared Source">
// Copyright (c) Sitecore.  All rights reserved.
// </copyright>
// <summary>Defines the SublayoutBase type.</summary>
// <license>
// http://sdn.sitecore.net/Resources/Shared%20Source/Shared%20Source%20License.aspx
// </license>
// <url>http://trac.sitecore.net/SublayoutParameterHelper/</url>
//-------------------------------------------------------------------------------------------------

namespace ColorBox4Sitecore.Helpers
{
    /// <summary>
    /// Base class for sublayouts that accept parameters and expose properties.
    /// </summary>
    public class SublayoutBase : System.Web.UI.UserControl
    {
        #region Private members
        /// <summary>
        /// The data source item for the sublayout (defaults to the context item).
        /// </summary>
        private Sitecore.Data.Items.Item _dataSourceItem;

        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets the data source item for the sublayout.
        /// </summary>
        public Sitecore.Data.Items.Item DataSourceItem
        {
            get
            {
                if (this._dataSourceItem == null)
                {
                    this._dataSourceItem = Sitecore.Context.Item;
                }

                return this._dataSourceItem;
            }

            set
            {
                this._dataSourceItem = value;
            }
        }

        /// <summary>
        /// Gets or sets the data source for the sublayout.
        /// Reads or updates <see cref="DataSourceItem" />.
        /// </summary>
        public string DataSource
        {
            get
            {
                if (this.DataSourceItem == null)
                {
                    return string.Empty;
                }

                return this.DataSourceItem.Paths.FullPath;
            }

            set
            {
                this._dataSourceItem = Sitecore.Context.Database.GetItem(value);
            }
        }

        #endregion

        #region Protected properties
        /// <summary>
        /// Gets or sets the SublayoutParameterHelper which helps parse sublayout parameters.
        /// </summary>
        protected SublayoutParameterHelper Helper { get; set; }

        #endregion

        #region Public methods
        /// <summary>
        /// Return the value of a parameter passed to the sublayout.
        /// </summary>
        /// <param name="name">The name of the parameter.</param>
        /// <returns>The value of the parameter, or an empty string.</returns>
        public string GetParameter(string name)
        {
            return this.Helper.GetParameter(name);
        }

        /// <summary>
        /// Returns the opening div attribute
        /// </summary>
        /// <param name="classes">the classes to append</param>
        /// <returns>the opening div attributes</returns>
        public string OpeningDivAttributes(string classes)
        {
            string attributes = string.Empty;

            string htmlId = this.GetParameter("Html Id");
            string htmlClasses = this.GetParameter("Html Classes");

            if (!string.IsNullOrEmpty(htmlId))
            {
                attributes = string.Format("id=\"{0}\"", htmlId);
            }

            if (!string.IsNullOrEmpty(htmlClasses))
            {
                classes += " " + htmlClasses.Replace(",", string.Empty);
            }

            if (!string.IsNullOrEmpty(classes))
            {
                 attributes += string.Format("class=\"{0}\"", classes);
            }

            return attributes;
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// OnInit method
        /// </summary>
        /// <param name="e">Event arguments</param>
        protected override void OnInit(System.EventArgs e)
        {
            base.OnInit(e);

            this.Helper =
              new SublayoutParameterHelper(this, true);
        }

        #endregion
    }
}